package com.cn.sys.main.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cn.sys.frame.util.page.PageData;
import com.cn.sys.frame.util.query.QueryBean;
import com.cn.sys.frame.util.query.QueryCondition;
import com.cn.sys.frame.web.BaseController;
import com.cn.sys.main.entity.SysOpeLog;
import com.cn.sys.main.service.SysOpeLogService;
import com.cn.sys.main.util.DictCache;

@Controller
@RequestMapping("sys/log")
public class SysOpeLogController extends BaseController {

	@Autowired
	private SysOpeLogService sysOpeLogService;
	
	//@InitBinder
	//protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
			//DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			//CustomDateEditor editor = new CustomDateEditor(df, false);
			//binder.registerCustomEditor(Date.class, editor);
	//}
	
	@RequestMapping(value = "/toList.do", method = RequestMethod.GET)
	public String toList(HttpServletRequest request, Model model){
		return "sys/log/logList";
	}
	
	@RequestMapping(value = "/list.do", method = RequestMethod.POST)
	@ResponseBody
	public Object list(HttpServletRequest request, String operateUser, String startDate, String endDate) throws Exception{
		PageData<SysOpeLog> pageData = createPageDataFromRequest(request);
		QueryCondition queryCondition = new QueryCondition();
		if(StringUtils.isNotBlank(operateUser)){
			QueryBean qb = new QueryBean(" AND T1.OPERATE_USER LIKE ?", "T1.OPERATE_USER", "%"+operateUser+"%");
			queryCondition.setQueryBean(qb);
		}
		//System.out.println("---------"+startDate+"  "+endDate);
		if(StringUtils.isNotBlank(startDate)){
			DateTime dateTime = new DateTime(startDate);
			QueryBean qb = new QueryBean(" AND T1.OPERATE_TIME >= ?", "T1.OPERATE_TIME", dateTime.toDate());
			queryCondition.setQueryBean(qb);
		}
		if(StringUtils.isNotBlank(endDate)){
			DateTime dateTime = new DateTime(endDate);
			QueryBean qb = new QueryBean(" AND T1.OPERATE_TIME <= ?", "T1.OPERATE_TIME", dateTime.toDate());
			queryCondition.setQueryBean(qb);
		}
		queryCondition.setOrderSql(" ORDER BY T1.OPERATE_TIME DESC");
		sysOpeLogService.getPageData(pageData, queryCondition);
		return pageData.getPageDataMap();
	}
	
	@RequestMapping(value = "/toView.do", method = RequestMethod.GET)
	public String toView(String id, Model model) throws Exception{
		if(StringUtils.isNotBlank(id)&&StringUtils.isNumeric(id)){
			SysOpeLog opeLog = sysOpeLogService.get(Long.valueOf(id));
			if(opeLog!=null){
				//opeLog.setOperateType(DictCache.convertCodeToName(opeLog.getOperateType(), "sys_log_operate_type"));
				//opeLog.setOperateObj(DictCache.convertCodeToName(opeLog.getOperateObj(), "sys_log_operate_obj"));
				model.addAttribute("operateType", DictCache.convertValToName(opeLog.getOperateType(), "sys_log_operate_type"));
				model.addAttribute("operateObj", DictCache.convertValToName(opeLog.getOperateObj(), "sys_log_operate_obj"));
			}
			model.addAttribute("opeLog", opeLog);
		}
		return "sys/log/logView";
	}
	
	@RequestMapping(value = "/delete.do", method = RequestMethod.POST)
	@ResponseBody
	public Object delete(HttpServletRequest request, String ids) throws Exception{
		Map<String, Object> result = new HashMap<String, Object>(2);
		if(StringUtils.isNotBlank(ids)){
			try {
				sysOpeLogService.delete(ids);
				result.put("success", true);
			} catch (Exception e) {
				//e.printStackTrace();
				result.put("success", false);
			}
		}else {
			result.put("success", false);
		}
		return result;
	}
}
